import javax.swing.JTextArea;
import java.io.*;
import java.util.ArrayList;

public class BattleManager implements Serializable {

	private Player player;
	private Character opponent;
	
	private int playerTurns;
	private int opponentTurns;
	
	private JTextArea feedback;
	private MoveListWindow CombatWindow;
	
	private ArrayList<StatusEffect> RemoveEffects = new ArrayList<StatusEffect>();

	
	public BattleManager(JTextArea y, MoveListWindow x) {
		feedback = y;
		CombatWindow = x;
	}
	
	public void engage(Player Player, Character Opponent, String aggressor) {
		player = Player;
		player.inCombat(true);
		opponent = Opponent;
		playerTurns = 100;
		opponentTurns = 100;
		player.setActionPoints(player.getMaxAP());
		if (aggressor.equalsIgnoreCase("NPC")) {
			opponent.setActionPoints(opponent.getMaxAP());
			feedback.append("You have been seen by " + opponent.getName() + "!\n" + opponent.getAttackMsg() + "\n");
			while(opponent.getActionPoints() > opponent.minActionPoints()) {
				opponent.attack(player, feedback, opponentTurns);
				if (player.getCurrentHP() <= 0) {
					player.Death();
					feedback.append("You have been slain by " + opponent.getName() + "!");
					opponent.setActionPoints(0);
				}
			}
		} else { 
		feedback.append("You attack " + opponent.getName() + ".");
		opponent.setActionPoints(0);
		}

	}
	
	public void ParseCmd(String a) {
		for (StatusEffect p : player.getEffects()) {
			feedback.append(p.tick(player) + "\n");
		}
		if (player.numActionPoints() > player.minActionPoints()) {
			if (a.equalsIgnoreCase("Attack") || a.equalsIgnoreCase("Heavy Blow") || a.equalsIgnoreCase("Stun") || a.equalsIgnoreCase("BattleShout")) {
				if (player.numActionPoints() < Math.abs(player.getAbility(a).getMove_Cost(player))) {
					feedback.append("You don't have enough movement points left to use this ability!");
				} else {
					player.getAbility(a).Fire(player, opponent, feedback, playerTurns);
					CombatWindow.Refresh(player);
				}
			}
		} else {
			feedback.append("You try to attack " + opponent.getName() + " but you are currently incapacitated!\n");
		}
			if (opponent.getCurrentHP() <= 0) {
				opponent.Death();
				feedback.append("You have slain " + opponent.getName() + "!");
				player.inCombat(false);
				player.setActionPoints(0.0);
				player.resetAbilities();
			}
			if (player.numActionPoints() < player.minActionPoints() && opponent.getCurrentHP() > 0) {
				
				opponent.startTurn();
				playerTurns = playerTurns + 1;
				CombatWindow.Refresh(player);
				if (opponent.getActionPoints() <= 0) {
					feedback.append("The " + opponent.getName() + " is stunned!");
				} else {
				while(opponent.getActionPoints() >= opponent.minActionPoints()) {
					opponent.attack(player, feedback, opponentTurns);
					if (player.getCurrentHP() <= 0) {
						player.Death();
						feedback.append("You have been slain by " + opponent.getName() + "!");
						opponent.setActionPoints(0);
					}
				}
			}
			player.startTurn();
			
			for (StatusEffect effect : player.getEffects()) {
				if (effect.checkExpired(playerTurns) == true) {
					RemoveEffects.add(effect);
				}
			}
			for (StatusEffect effect : RemoveEffects) {
				player.removeEffect(effect);
			}
			RemoveEffects.clear();

			opponentTurns = opponentTurns + 1;
			CombatWindow.Refresh(player);
			

			
			}
			
			
		
	}
	
}
